<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author Cheng
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'unitmanage:unitBuilding:edit'"
    @register="registerDrawerDetail"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsUnitmanageUnitBuildingForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { UnitBuilding, unitBuildingSave, unitBuildingForm } from '/@/api/unitmanage/unitBuilding';
  import {Office, officeTreeData,officeListData,officeTreeDataList} from '/@/api/sys/office';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('unitmanage.unitBuilding');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<UnitBuilding>({} as UnitBuilding);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.tid ? t('详情') : t('详情'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('所属主体'),
      field: 'enterpriseName',
      fieldLabel: 'enterpriseName',
      component: 'TreeSelect',
      componentProps: {
        maxlength: 255,
        api:officeTreeData,
        params:{
        
        },
        onChange:(officeName,officeCode)=>{
          record.value.unitCode=officeCode,
          record.value.enterpriseName=officeName
        },
        allowClear: true,
        disabled:true
      },
    },
    {
      label: t('建筑名称'),
      field: 'name',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled:true
      },
    },
    {
      label: t('详细地址'),
      field: 'location',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled:true
      },
    },
    // {
    //   label: t('消防引擎数量'),
    //   field: 'fireEngineNum',
    //   component: 'Input',
    //   rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    // },
    // {
    //   label: t('设备数量'),
    //   field: 'equipmentNum',
    //   component: 'Input',
    //   rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    // },
    {
      label: t('占地面积'),
      field: 'occupyArea',
      component: 'Input',
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
      componentProps: {
        disabled:true
      }
    },
    {
      label: t('建筑类型'),
      field: 'buildingType',
      component: 'Select',
      componentProps: {
        maxlength: 100,
        options:[
          {label:"民用建筑",value:"11001"},
          {label:"工业建筑",value:"11002"},
          {label:"仓库建筑",value:"11003"},
          {label:"储罐建筑",value:"11004"},
          {label:"堆场",value:"11005"},
          {label:"装置",value:"11006"},
        ],
        disabled:true
      },
    },
    {
      label: t('周边建筑'),
      field: 'unitAbutSituation',
      component: 'TreeSelect',
      componentProps:{
        api:officeTreeData,
        allowClear: true,
        disabled:true
      }
    },
    // {
    //   label: t('添加时间'),
    //   field: 'addTime',
    //   component: 'DatePicker',
    //   componentProps: {
    //     format: 'YYYY-MM-DD HH:mm',
    //     showTime: { format: 'HH:mm' },
    //   },
    // },
    // {
    //   label: t('更新时间'),
    //   field: 'updateTime',
    //   component: 'DatePicker',
    //   componentProps: {
    //     format: 'YYYY-MM-DD HH:mm',
    //     showTime: { format: 'HH:mm' },
    //   },
    // },
    // {
    //   label: t('逻辑删除'),
    //   field: 'deleted',
    //   component: 'Input',
    // },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { md: 24, lg: 12 },
  });

  const [registerDrawerDetail, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await unitBuildingForm(data);
    record.value = (res.unitBuilding || {}) as UnitBuilding;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        // isNewRecord: record.value.isNewRecord,
        tid: record.value.tid,
        enterpriseName:record.value.enterpriseName,
        unitCode:record.value.unitCode
      };
      // console.log('submit', params, data, record);
      const res = await unitBuildingSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(error.message || t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
